package com.exposit.zbs.model;

import java.time.Period;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class GrantApplication implements java.io.Serializable {

	private Long grantId;
	private GrantArea grantArea;
	private GrantApplicationStatus grantApplicationStatus;
	private User user;
	private Long requestedMoney;
	private Long takenMoney;
	private Date releaseDate;
	private Date reviewDate;
	private String grantName;
	private String requestDescription;
	private Boolean isOpened;
	private Boolean isDeleted;
	private List<Comment> comments = new ArrayList<Comment>(0);
	private List<MoneyTransfer> moneyTransfer = new ArrayList<MoneyTransfer>(0);
	private Date creationDate;
	private List<Like> likes = new ArrayList<Like>(0);
	private List<GrantChangelog> changelog = new ArrayList<GrantChangelog>(0);
	private List<MoneyTransferFromFund> moneyTransfersFromFund = new ArrayList<MoneyTransferFromFund>(0);
	public GrantApplication() {

	}

	public List<GrantChangelog> getChangelog() {

		return changelog;
	}

	public void setChangelog(List<GrantChangelog> changelog) {

		this.changelog = changelog;
	}

	public List<MoneyTransferFromFund> getMoneyTransfersFromFund() {

		return moneyTransfersFromFund;
	}

	public void setMoneyTransfersFromFund(List<MoneyTransferFromFund> moneyTransfersFromFund) {

		this.moneyTransfersFromFund = moneyTransfersFromFund;
	}

	public Long getGrantId() {

		return this.grantId;
	}

	public void setGrantId(Long grantId) {

		this.grantId = grantId;
	}

	public GrantArea getGrantArea() {

		return this.grantArea;
	}

	public void setGrantArea(GrantArea grantArea) {

		this.grantArea = grantArea;
	}

	public GrantApplicationStatus getGrantApplicationStatus() {

		return this.grantApplicationStatus;
	}

	public void setGrantApplicationStatus(GrantApplicationStatus grantApplicationStatus) {

		this.grantApplicationStatus = grantApplicationStatus;
	}

	public User getUser() {

		return this.user;
	}

	public void setUser(User user) {

		this.user = user;
	}

	public Date getReviewDate() {

		return this.reviewDate;
	}

	public void setReviewDate(Date reviewDate) {

		this.reviewDate = reviewDate;
	}

	public String getGrantName() {

		return this.grantName;
	}

	public void setGrantName(String grantName) {

		this.grantName = grantName;
	}

	public String getRequestDescription() {

		return this.requestDescription;
	}

	public void setRequestDescription(String requestDescription) {

		this.requestDescription = requestDescription;
	}

	public Boolean getIsOpened() {

		return this.isOpened;
	}

	public void setIsOpened(Boolean isOpened) {

		this.isOpened = isOpened;
	}

	public Boolean getIsDeleted() {

		return this.isDeleted;
	}

	public void setIsDeleted(Boolean isDeleted) {

		this.isDeleted = isDeleted;
	}

	public List<Comment> getComments() {

		return comments;
	}

	public List<MoneyTransfer> getMoneyTransfer() {

		return this.moneyTransfer;
	}

	public void setMoneyTransfer(List<MoneyTransfer> moneyTransfer) {

		this.moneyTransfer = moneyTransfer;
	}

	public List<Like> getLikes() {

		return likes;
	}

	public void setLikes(List<Like> likes) {

		this.likes = likes;
	}

	public Date getCreationDate() {

		return creationDate;
	}

	public void setCreationDate(Date creationDate) {

		this.creationDate = creationDate;
	}

	public Like isLikedByLogin(String login) {

		for (Like like : likes) {
			if (like.getUser().getUserLogin().equals(login)) return like;
		}
		return null;
	}

	public Long getTakenMoney() {

		return takenMoney;
	}

	public void setTakenMoney(Long takenMoney) {

		this.takenMoney = takenMoney;
	}

	public int getDaysToGo() {

		Date currentDate = new Date();
		java.time.Period period = Period.between(currentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(), this.getReleaseDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
		return period.getDays() + period.getMonths() * 30 + period.getYears() * 365;
	}

	public Date getReleaseDate() {

		return this.releaseDate;
	}

	public void setReleaseDate(Date releaseDate) {

		this.releaseDate = releaseDate;
	}

	public double getCompletedPercent() {

		return this.getMoneyAmount() * 100 / this.getRequestedMoney();
	}

	public Long getRequestedMoney() {

		return this.requestedMoney;
	}

	public void setRequestedMoney(Long requestedMoney2) {

		this.requestedMoney = requestedMoney2;
	}

	public long getMoneyAmount() {

		long result = 0;
		for (MoneyTransfer mt : this.moneyTransfer) {
			result += mt.getMoneySum();
		}
		result += getAmountFromFund();
		return result;
	}

	public long getAmountFromFund() {

		long result = 0;
		for (MoneyTransferFromFund mt : this.moneyTransfersFromFund) {
			result += mt.getMoneyAmount();
		}
		return result;
	}

}
